# This code is part of the MWP System
# Copyright (c) 2012 Benjamin Lubin (blubin@bu.com) 
# Published under and subject to the GPLv2 license available at http://www.gnu.org/licenses/gpl-2.0.html

'''
Created on Dec 13, 2012
@author: blubin
'''

from backend.backend import Backend;
from workflow.task import Task;
from workflow.result import Result;
from workflow.flowData import Status;
from unique import UNIQUE;

class CallCenterBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "CallCenter"+UNIQUE, dashboard=True);

    def wire(self):
        self.register_result_listener("Customer", "EnterProblem", self.problem_entered)
        self.register_result_listener("Brian", "EvaluateProblem", self.evaluation_complete)
        self.register_result_listener("Brian", "ProvideAnswer", self.answer_complete)
#         self.register_result_listener("Brian", "AskManager", self.ask_manager)
        self.register_result_listener("Manager", "ProvideSuperAnswer", self.super_answer_complete)
        self.register_result_listener("Customer", "AcknowledgeAnswer", self.problem_complete)

    def problem_entered(self, results):
        print "backend: problem_entered"
        for result in results:
            task = Task.construct_from_result(result, "Brian", "EvaluateProblem");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def evaluation_complete(self, results):
        print "backend: evaluation_complete"
        for result in results:
            if result.data['brianknowsanswer'] == "TRUE":
                task = Task.construct_from_result(result, "Brian", "ProvideAnswer");
            else:    
                task = Task.construct_from_result(result, "Manager", "ProvideSuperAnswer");                
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def answer_complete(self, results):
        print "backend: answer_complete"
        for result in results:
            task = Task.construct_from_result(result, "Customer", "AcknowledgeSolution");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

#     def ask_manager(self, results):
#         for result in results:
#             task = Task.construct_from_result(result, "Manager", "ProvideSuperAnswer");
#             self.workflow.add(task);
#             self.workflow.update_status(result, Status.COMPLETE);

    def super_answer_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Brian", "ProvideAnswer");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def problem_complete(self, results):
        for result in results:
            self.workflow.update_status(result, Status.COMPLETE);
               
if __name__ == '__main__':
    backend = CallCenterBackend();